package com.kotlin.android.simple.simple2

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.kotlin.android.api.CallResult
import com.kotlin.android.api.RetrofitManager
import com.kotlin.android.api.api.ApiMisc
import com.kotlin.android.api.bean.SeatBanner
import com.kotlin.android.api.repository.SimpleRepository
import com.kotlin.android.api.base.BaseUIModel
import com.kotlin.android.api.call.SimpleCallResult
import com.kotlin.android.core.BaseViewModel
import kotlinx.coroutines.launch

/**
 *
 * Created on 2020/4/24.
 *
 * @author o.s
 */
class SimpleViewModel2 : BaseViewModel() {
    private val repo by lazy { SimpleRepository() }
    private val api by lazy { RetrofitManager.api(ApiMisc::class.java) }

    private val _uiState = MutableLiveData<SimpleUIModel>()

    val uiState: LiveData<SimpleUIModel> = _uiState

    /**
     * 情况一：
     * 使用 [CallResult.handleData] 级联调用方法的方式，处理数据逻辑
     */
    fun loadData() {
        viewModelScope.launch(main) {
            CallResult<SeatBanner>().refresh {
                emitUIState(isRefresh = true)
            }.loading {
                emitUIState(showLoading = true)
            }.empty {
                emitUIState(isEmpty = true)
            }.netError {
                emitUIState(netError = it)
            }.error {
                emitUIState(error = it)
            }.success {
                emitUIState(success = it)
            }.handleData {
                when (it.bizCode) {
                    0 -> {
                        success?.invoke(it)
                    }
                    1 -> {
                        error?.invoke(it.bizMsg)
                    }
                    2 -> {
                        error?.invoke(it.bizMsg)
                    }
                    else -> {
                        error?.invoke("其他情况")
                    }
                }
            }.call(errorMessage = "自定义错误文案") {
                api.getSeatBanner()
            }
        }
    }

    /**
     * 情况二：
     * 使用 [CallResult] 子类扩展的方式，处理数据逻辑
     */
    fun loadData2() {
        viewModelScope.launch(main) {
            SimpleCallResult().refresh {
                emitUIState(isRefresh = true)
            }.loading {
                emitUIState(showLoading = true)
            }.empty {
                emitUIState(isEmpty = true)
            }.netError {
                emitUIState(netError = it)
            }.error {
                emitUIState(error = it)
            }.success {
                emitUIState(success = it)
            }.call(errorMessage = "自定义错误文案") {
                api.getSeatBanner()
            }
        }
    }

    /**
     * 发出UI状态
     */
    private fun emitUIState(
        showLoading: Boolean = false,
        isRefresh: Boolean = false,
        loadMore: Boolean = false,
        error: String? = null,
        netError: String? = null,
        isEmpty: Boolean = false,
        success: SeatBanner? = null
    ) {
        val model = SimpleUIModel()
        model.showLoading = showLoading
        model.isRefresh = isRefresh
        model.loadMore = loadMore
        model.error = error
        model.netError = netError
        model.isEmpty = isEmpty
        model.success = success

        _uiState.value = model
    }

    /**
     * 根据实际的接口业务要求，定制（增加）该对象的字段属性：
     */
    class SimpleUIModel : BaseUIModel<SeatBanner>()
}